package org.streets.database.datadict.impl;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Id;

import org.streets.commons.util.StringUtils;
import org.streets.database.SQLUtils;
import org.streets.database.annotations.DataDictDef;
import org.streets.database.datadict.ColumnDefinition;
import org.streets.database.datadict.TableDefinition;

/**
 * 
 * @author dzb
 *
 */
public class ColumnDefinitionImpl implements ColumnDefinition, Serializable {
	
	private static final long serialVersionUID = 8641000650555673252L;
	
	private TableDefinition table;
	private String name;
	private String alias;
	private Class<?> type;
	private String attr; //确定是否代码值,[选择值]是否外键值,[弹出窗口选择,如何确定弹出哪一个？]
	private String folder;
	private String memo;
	private boolean encrypt;

	ColumnDefinitionImpl(TableDefinition table, Id id, Column column, DataDictDef def, String fieldName, Class<?> type) {
		this.table = table;
		this.type = type;
		this.alias = def.alias();
		if (id != null) {
			attr = "P";
		} else if (StringUtils.isNotEmpty(def.attr())) {
			attr = def.attr();
		} else {
			attr = "N";
		}		
		if (column == null || StringUtils.isEmpty(column.name())) {
			this.name = SQLUtils.convPropertyNameToColumnName(fieldName);
		} else {
			this.name = column.name();
		}		
		this.folder = def.category();
		this.memo = def.memo();
		this.encrypt = def.encrypt();
	}
	
	public TableDefinition getTable() {
		return table;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#getAlias()
	 */
	public String getAlias() {
		return alias;
	}
	
	public void setAlias(String alias) {
		this.alias = alias;
	}
	
	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#getType()
	 */
	public Class<?> getType() {
		return type;
	}
	
	public void setType(Class<?> type) {
		this.type = type;
	}
	
	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#getAttr()
	 */
	public String getAttr() {
		return attr;
	}
	
	public void setAttr(String ext) {
		this.attr = ext;
	}
	
	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#getFolder()
	 */
	public String getFolder() {
		return folder;
	}
	
	public void setFolder(String folder) {
		this.folder = folder;
	}

	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#isEncrypt()
	 */
	public boolean isEncrypt() {
		return encrypt;
	}

	public void setEncrypt(boolean encrypt) {
		this.encrypt = encrypt;
	}
	
	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#getMemo()
	 */
	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
	
	/* (non-Javadoc)
	 * @see org.streets.database.datadict.impl.ColumnDefinition#equals(java.lang.Object)
	 */
	@Override
    public boolean equals(Object object) {
        if (!(object instanceof ColumnDefinitionImpl)) {
            return false;
        }
        ColumnDefinitionImpl other = (ColumnDefinitionImpl) object;
        return this.table.equals(other.table) && this.name.equals(other.name);
    }
}
